Page({
  data: {
    currentDate: new Date().getTime(),
    houseLayerVisible: false,
    repairLayerVisible: false,
    dateLayerVisible: false,
    fileList: [],
    // 保修房屋
    houseList: [],
    // 在页面中展示的保修房屋
    houseInfo: '',
    // 维修项目
    repairItem: [],
    // 在页面中展示的维修项目
    repairInfo: '',
    // 在页面中展示的日期
    appointment: '',
    houseId: '',
    repairItemId: '',
    mobile: '',
    description: ''
  },
  onLoad ({ id }) {
    if (id) {
      this.getDetail(id)
      wx.setNavigationBarTitle({
        title: '修改报修信息'
      })
      this.setData({ id })
    }
    this.getHouseList()
    this.getRepairItem()
  },
  verifyHouse () {
    const valid = this.data.houseId !== ''
    // 验证结果提示
    if (!valid) wx.$utils.toast('请选择房屋信息!')
    // 返回验证结果
    return valid
  },
  verifyRepair () {
    const valid = this.data.repairItemId !== ''
    // 验证结果提示
    if (!valid) wx.$utils.toast('请选择维修项目!')
    // 返回验证结果
    return valid
  },
  verifyMobile () {
    // 验证手机号
    const reg = /^[1][3-8][0-9]{9}$/
    const valid = reg.test(this.data.mobile.trim())
    // 验证结果提示
    if (!valid) wx.$utils.toast('请填写正确的手机号码!')
    // 返回验证结果
    return valid
  },
  verifyDate () {
    // 验证日期格式
    const reg = /^\d{4}\/\d{2}\/\d{2}$/
    const valid = reg.test(this.data.appointment)
    // 验证结果提示
    if (!valid) wx.$utils.toast('请选择预约日期!')
    // 返回验证结果
    return valid
  },
  verifyDescription () {
    // 验证报修项目描述
    const valid = this.data.description.trim() !== ''
    // 验证结果提示
    if (!valid) wx.$utils.toast('请填写问题描述!')
    // 返回验证结果
    return valid
  },
  // 数据回显
  async getDetail (id) {
    const res = await wx.$http.get(`/repair/${id}`)
    console.log(res)
    if (res.code !== 10000) return wx.$utils.toast()
    this.setData({
      ...res.data,
      repairInfo: res.data.repairItemName,
      fileList: res.data.attachment
    })
  },
  // 提交表单

  async submit () {
    // 逐个验证表单数据
    if (!this.verifyHouse()) return
    if (!this.verifyRepair()) return
    if (!this.verifyMobile()) return
    if (!this.verifyDate()) return
    if (!this.verifyDescription()) return

    const { id, houseId, repairItemId, mobile, description, appointment, fileList: attachment } = this.data
    const res = await wx.$http.post('/repair', {
      houseId,
      repairItemId,
      mobile,
      description,
      appointment,
      attachment,
      id
    })
    if (res.code !== 10000) return wx.$utils.toast()
    this.goList()
  },
  // 获取能保修的房屋列表
  async getHouseList () {
    const res = await wx.$http.get('/house')
    if (res.code !== 10000) return wx.$utils.toast()
    this.setData({
      houseList: res.data
    })
  },
  // 选择保修房屋
  selectHouse (e) {
    this.setData({
      houseInfo: e.detail.name,
      houseId: e.detail.id
    })
  },
  // 获取维修项目
  async getRepairItem () {
    const res = await wx.$http.get('/repairItem')
    this.setData({
      repairItem: res.data
    })
  },
  // 选择维修项目
  selectRepair (e) {
    this.setData({
      repairInfo: e.detail.name,
      repairItemId: e.detail.id
    })
  },
  // 选择日期
  selectDate (e) {
    this.setData({
      appointment: wx.$utils.formatDate(e.detail)
    })
    this.closeDateLayer()
  },
  // 上传图片
  selectImage (e) {
    wx.uploadFile({
      url: `${wx.$http.baseURL}/upload`,
      name: 'file',
      filePath: e.detail.file.url,
      header: {
        Authorization: getApp().token
      },
      success: res => {
        const { data } = JSON.parse(res.data)
        this.data.fileList.push({ url: data.url })
        this.setData({
          fileList: this.data.fileList
        })
      }
    })
  },
  // 删除图片
  deleteImage (e) {
    console.log(e.detail.index)
    this.setData({
      fileList: this.data.fileList.filter((ev, i) => i !== e.detail.index)
    })
  },
  openHouseLayer () {
    this.setData({ houseLayerVisible: true })
  },
  closeHouseLayer () {
    this.setData({ houseLayerVisible: false })
  },
  openRepairLayer () {
    this.setData({ repairLayerVisible: true })
  },
  closeRepairLayer () {
    this.setData({
      repairLayerVisible: false
    })
  },
  openDateLayer () {
    this.setData({ dateLayerVisible: true })
  },
  closeDateLayer () {
    this.setData({ dateLayerVisible: false })
  },
  goList () {
    wx.reLaunch({
      url: '/repair_pkg/pages/list/index'
    })
  }
})
